package com.zhm.demo.duckdb;

import com.zhm.demo.duckdb.cfg.DuckDbCfg;
import com.zhm.demo.duckdb.entity.CsvEntity;
import com.zhm.demo.duckdb.mapper.CsvMapper;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

public class SimpleMyBatis {

    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.addMapper(CsvMapper.class);
//        config.addMappers(DuckDbCfg.BASE_PACKAGES);

        DataSource dataSource = new DuckDbCfg().dataSource();
        Environment environment = new Environment("test", new SpringManagedTransactionFactory(), dataSource);
        config.setEnvironment(environment);
        // 2. 创建 SqlSessionFactory

        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(config);

        // 3. 获取 SqlSession
        try (SqlSession session = sqlSessionFactory.openSession()) {
            // 4. 获取 Mapper 接口实例
            CsvMapper mapper = session.getMapper(CsvMapper.class);

            // 5. 执行查询
            List<Map<String,String>> userMaps = mapper.selectAll();
//            List<CsvEntity> users = mapper.selectAllByXml();
//            users.forEach(System.out::println);

            session.commit(); // 手动提交事务
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
